package com.wyzz.hr.report.bo.impl;

import java.math.BigDecimal;
import java.text.*;
import java.util.*;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;

import com.wyzz.hr.base.Status;
import com.wyzz.hr.bo.IDepartmentBO;
import com.wyzz.hr.bo.ILeavecalendarBO;
import com.wyzz.hr.bo.ILocationBO;
import com.wyzz.hr.dao.ILeaverequestDAO;
import com.wyzz.hr.dao.IOvertimerequestDAO;
import com.wyzz.hr.pojo.Department;
import com.wyzz.hr.pojo.Leaverequest;
import com.wyzz.hr.pojo.Leavetype;
import com.wyzz.hr.pojo.Location;
import com.wyzz.hr.pojo.Overtimerequest;
import com.wyzz.hr.pojo.Overtimetype;
import com.wyzz.hr.report.bo.IReportBo;
import com.wyzz.hr.report.dao.IReportDAO;
import com.wyzz.hr.util.BaseCrit;

public class ReportBoImpl implements IReportBo {

	public ReportBoImpl() {
	}

	public List getObjects(Class clas, String fetch[]) {
		List result = reportDAO.getObjects(clas, fetch);
		return result;
	}

	/**
	 * @deprecated Method getEmpLRHistory is deprecated
	 */

	public List getEmpLRHistory(String startTime, String endTime,
			String lrType, int lrStatusInclude, String group) {
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		java.util.Date startDate = null;
		java.util.Date endDate = null;
		try {
			startDate = df.parse(startTime);
			endDate = df.parse(endTime);
			Calendar c = Calendar.getInstance();
			c.setTime(startDate);
			c.set(5, c.getActualMinimum(5));
			startDate = c.getTime();
			c.setTime(endDate);
			c.set(5, c.getActualMaximum(5));
			c.set(11, c.getActualMaximum(11));
			endDate = c.getTime();
		} catch (ParseException e) {
			e.printStackTrace();
			return null;
		}
		DetachedCriteria detachedCrteria = DetachedCriteria
				.forClass(Leaverequest.class);
		detachedCrteria.add(Restrictions.ge(Leaverequest.PROP_LR_START_DATE,
				startDate));
		detachedCrteria.add(Restrictions.lt(Leaverequest.PROP_LR_END_DATE,
				endDate));
		if (!lrType.equals("all"))
			detachedCrteria.add(Restrictions.eq(Leaverequest.PROP_LR_LT_NO,
					new Leavetype(lrType)));
		if (lrStatusInclude == 1)
			BaseCrit.addDC(detachedCrteria, Leaverequest.PROP_LR_STATUS, "in",
					Status.PROCESS_APPROVED);
		else
			BaseCrit.addDC(detachedCrteria, Leaverequest.PROP_LR_STATUS, "in",
					Status.PROCESSED);
		List lrList = lr_DAO.findByCriteria(detachedCrteria);
		Calendar startCalendar = Calendar.getInstance();
		startCalendar.set(5, startCalendar.getActualMinimum(5));
		Calendar endCalendar = Calendar.getInstance();
		endCalendar.set(5, endCalendar.getActualMaximum(5));
		startCalendar.setTime(startDate);
		endCalendar.setTime(endDate);
		int month = (endCalendar.get(2) - startCalendar.get(2)) + 1;
		TreeMap listMap = new TreeMap(new ExaminCellCompare());
		if (group.equals("dept")) {
			List dList = d_BO.FindEnabledDepartment();
			for (int j = 0; j < dList.size(); j++) {
				for (int i = 0; i < month; i++)
					listMap.put((new StringBuilder()).append("|").append(
							startCalendar.get(2) + i).append("|").append(
							((Department) dList.get(j)).getId()).toString(),
							new TableCell((new StringBuilder()).append(
									startCalendar.get(2) + i + 1).append(
									" \u6708").toString(), ((Department) dList
									.get(j)).getDepartmentName()));

			}

		} else if (group.equals("location")) {
			List lList = l_BO.FindAllLocation();
			for (int j = 0; j < lList.size(); j++) {
				for (int i = 0; i < month; i++)
					listMap.put((new StringBuilder()).append("|").append(
							startCalendar.get(2) + i).append("|").append(
							((Location) lList.get(j)).getId()).toString(),
							new TableCell((new StringBuilder()).append(
									startCalendar.get(2) + i + 1).append(
									" \u6708").toString(), ((Location) lList
									.get(j)).getLocationName()));

			}

		} else {
			for (int i = 0; i < month; i++)
				listMap.put((new StringBuilder()).append("|").append(
						startCalendar.get(2) + i).toString(), new TableCell(
						(new StringBuilder()).append(
								startCalendar.get(2) + i + 1).append(" \u6708")
								.toString()));

		}
		try {
			if (group.equals("dept")) {
				for (int i = 0; i < lrList.size(); i++) {
					Leaverequest tempLr = (Leaverequest) lrList.get(i);
					Calendar currentC = Calendar.getInstance();
					currentC.setTime(tempLr.getLrStartDate());
					TableCell currentCell = (TableCell) listMap
							.get((new StringBuilder()).append("|").append(
									currentC.get(2)).append("|").append(
									tempLr.getLrEmpDept().getId()).toString());
					if (currentCell != null)
						currentCell.setValue(currentCell.getValue()
								+ tempLr.getLrTotalDays().doubleValue());
				}

			} else if (group.equals("location")) {
				for (int i = 0; i < lrList.size(); i++) {
					Leaverequest tempLr = (Leaverequest) lrList.get(i);
					Calendar currentC = Calendar.getInstance();
					currentC.setTime(tempLr.getLrStartDate());
					TableCell currentCell = (TableCell) listMap
							.get((new StringBuilder()).append("|").append(
									currentC.get(2)).append("|").append(
									tempLr.getLrEmpLocationNo().getId())
									.toString());
					if (currentCell != null)
						currentCell.setValue(currentCell.getValue()
								+ tempLr.getLrTotalDays().doubleValue());
				}

			} else {
				for (int i = 0; i < lrList.size(); i++) {
					Leaverequest tempLr = (Leaverequest) lrList.get(i);
					Calendar currentC = Calendar.getInstance();
					currentC.setTime(tempLr.getLrStartDate());
					TableCell currentCell = (TableCell) listMap
							.get((new StringBuilder()).append("|").append(
									currentC.get(2)).toString());
					if (currentCell != null)
						currentCell.setValue(currentCell.getValue()
								+ tempLr.getLrTotalDays().doubleValue());
				}

			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		List returnList = new ArrayList();
		for (Iterator celliterator = listMap.values().iterator(); celliterator
				.hasNext(); returnList.add(celliterator.next()))
			;
		return returnList;
	}

	/**
	 * @deprecated Method getEmpLOTHistory is deprecated
	 */

	public List getEmpLOTHistory(String startTime, String endTime,
			String lrType, int lrStatusInclude, String group) {
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		java.util.Date startDate = null;
		java.util.Date endDate = null;
		try {
			startDate = df.parse(startTime);
			endDate = df.parse(endTime);
			Calendar c = Calendar.getInstance();
			c.setTime(startDate);
			c.set(5, c.getActualMinimum(5));
			startDate = c.getTime();
			c.setTime(endDate);
			c.set(5, c.getActualMaximum(5));
			c.set(11, c.getActualMaximum(11));
			endDate = c.getTime();
		} catch (ParseException e) {
			e.printStackTrace();
			return null;
		}
		DetachedCriteria detachedCrteria = DetachedCriteria
				.forClass(Overtimerequest.class);
		detachedCrteria.add(Restrictions.ge(Overtimerequest.PROP_OR_END_DATE,
				startDate));
		detachedCrteria.add(Restrictions.le(Overtimerequest.PROP_OR_START_DATE,
				endDate));
		if (!lrType.equals("all"))
			detachedCrteria.add(Restrictions.eq(Overtimerequest.PROP_OR_OT_NO,
					new Overtimetype(lrType)));
		if (lrStatusInclude == 1)
			BaseCrit.addDC(detachedCrteria, Overtimerequest.PROP_OR_STATUS,
					"in", Status.PROCESS_APPROVED);
		else
			BaseCrit.addDC(detachedCrteria, Overtimerequest.PROP_OR_STATUS,
					"in", Status.PROCESSED);
		List otList = lr_DAO.findByCriteria(detachedCrteria);
		Calendar startCalendar = Calendar.getInstance();
		startCalendar.set(5, startCalendar.getActualMinimum(5));
		Calendar endCalendar = Calendar.getInstance();
		endCalendar.set(5, endCalendar.getActualMaximum(5));
		startCalendar.setTime(startDate);
		endCalendar.setTime(endDate);
		int month = (endCalendar.get(2) - startCalendar.get(2)) + 1;
		TreeMap listMap = new TreeMap(new ExaminCellCompare());
		if (group.equals("dept")) {
			List dList = d_BO.FindEnabledDepartment();
			for (int j = 0; j < dList.size(); j++) {
				for (int i = 0; i < month; i++)
					listMap.put((new StringBuilder()).append("|").append(
							startCalendar.get(2) + i).append("|").append(
							((Department) dList.get(j)).getId()).toString(),
							new TableCell((new StringBuilder()).append(
									startCalendar.get(2) + i + 1).append(
									" \u6708").toString(), ((Department) dList
									.get(j)).getDepartmentName()));

			}

		} else if (group.equals("location")) {
			List lList = l_BO.FindAllLocation();
			for (int j = 0; j < lList.size(); j++) {
				for (int i = 0; i < month; i++)
					listMap.put((new StringBuilder()).append("|").append(
							startCalendar.get(2) + i).append("|").append(
							((Location) lList.get(j)).getId()).toString(),
							new TableCell((new StringBuilder()).append(
									startCalendar.get(2) + i + 1).append(
									" \u6708").toString(), ((Location) lList
									.get(j)).getLocationName()));

			}

		} else {
			for (int i = 0; i < month; i++)
				listMap.put((new StringBuilder()).append("|").append(
						startCalendar.get(2) + i).toString(), new TableCell(
						(new StringBuilder()).append(
								startCalendar.get(2) + i + 1).append(" \u6708")
								.toString()));

		}
		try {
			if (group.equals("dept")) {
				for (int i = 0; i < otList.size(); i++) {
					Overtimerequest tempOt = (Overtimerequest) otList.get(i);
					Calendar currentC = Calendar.getInstance();
					currentC.setTime(tempOt.getOrStartDate());
					TableCell currentCell = (TableCell) listMap
							.get((new StringBuilder()).append("|").append(
									currentC.get(2)).append("|").append(
									tempOt.getOrEmpDept().getId()).toString());
					currentCell.setValue(currentCell.getValue()
							+ tempOt.getOrTotalHours().doubleValue());
				}

			} else if (group.equals("location")) {
				for (int i = 0; i < otList.size(); i++) {
					Overtimerequest tempOt = (Overtimerequest) otList.get(i);
					Calendar currentC = Calendar.getInstance();
					currentC.setTime(tempOt.getOrStartDate());
					TableCell currentCell = (TableCell) listMap
							.get((new StringBuilder()).append("|").append(
									currentC.get(2)).append("|").append(
									tempOt.getOrEmpLocationNo().getId())
									.toString());
					currentCell.setValue(currentCell.getValue()
							+ tempOt.getOrTotalHours().doubleValue());
				}

			} else {
				for (int i = 0; i < otList.size(); i++) {
					Overtimerequest tempOt = (Overtimerequest) otList.get(i);
					Calendar currentC = Calendar.getInstance();
					currentC.setTime(tempOt.getOrStartDate());
					TableCell currentCell = (TableCell) listMap
							.get((new StringBuilder()).append("|").append(
									currentC.get(2)).toString());
					currentCell.setValue(currentCell.getValue()
							+ tempOt.getOrTotalHours().doubleValue());
				}

			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		List returnList = new ArrayList();
		for (Iterator celliterator = listMap.values().iterator(); celliterator
				.hasNext(); returnList.add(celliterator.next()))
			;
		return returnList;
	}

	public List exeHqlList(String hqlStr) {
		return reportDAO.exeHqlList(hqlStr);
	}

	public IReportDAO getReportDAO() {
		return reportDAO;
	}

	public void setReportDAO(IReportDAO reportDAO) {
		this.reportDAO = reportDAO;
	}

	public ILeaverequestDAO getLr_DAO() {
		return lr_DAO;
	}

	public void setLr_DAO(ILeaverequestDAO lr_DAO) {
		this.lr_DAO = lr_DAO;
	}

	public ILeavecalendarBO getLc_BO() {
		return lc_BO;
	}

	public void setLc_BO(ILeavecalendarBO lc_BO) {
		this.lc_BO = lc_BO;
	}

	public IDepartmentBO getD_BO() {
		return d_BO;
	}

	public void setD_BO(IDepartmentBO d_bo) {
		d_BO = d_bo;
	}

	public ILocationBO getL_BO() {
		return l_BO;
	}

	public void setL_BO(ILocationBO l_bo) {
		l_BO = l_bo;
	}

	public IOvertimerequestDAO getOt_DAO() {
		return ot_DAO;
	}

	public void setOt_DAO(IOvertimerequestDAO ot_DAO) {
		this.ot_DAO = ot_DAO;
	}

	private IReportDAO reportDAO;
	private ILeaverequestDAO lr_DAO;
	private IOvertimerequestDAO ot_DAO;
	private ILeavecalendarBO lc_BO;
	private IDepartmentBO d_BO;
	private ILocationBO l_BO;
}
